﻿Imports Cls_Common
Imports VSModel
Imports System.Data
Partial Class FrontEnd_Control_Menu_LeftMenu
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hidCatId.Value = Page.RouteData.Values("Id")
            BindMenu()
        End If
    End Sub
    Protected Sub BindMenu()
        Using Data As New VSEntities
            Dim p = (From q In Data.Categories Where q.ParentId = 0 Select q Order By q.Sort).Take(20).ToList
            With rptMenu
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub


    Protected Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & "mua-ban/" & row.QueryString & "-" & row.CatId & ".html"
        If Page.RouteData.Values("Id") = row.CatId Then
            hplTitle.CssClass = "active"
        End If
        Dim rptSubCat As Repeater = CType(e.Item.FindControl("rptSubCat"), Repeater)
        Dim rptSubCat2 As Repeater = CType(e.Item.FindControl("rptSubCat2"), Repeater)
        Dim MenuSub = e.Item.FindControl("MenuSub")

        Using Data As New VSEntities
            'Lay toan bo cap con cua Cate chia thanh 2 cot
            Dim count As Integer = (From q In Data.Categories Where q.ParentId = row.CatId Select q.CatId).Count
            Dim half As Integer = count \ 2
            Dim p = (From q In Data.Categories Where q.ParentId = row.CatId).ToList

            If p.Count > 0 Then
                rptSubCat.DataSource = p.Take(half)
                rptSubCat.DataBind()

                rptSubCat2.DataSource = p.Skip(half).ToList
                rptSubCat2.DataBind()
            Else
                MenuSub.Visible = False
            End If
        End Using


    End Sub
    Protected Sub rptSubCat_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        Dim hplSubTitle As HyperLink = CType(e.Item.FindControl("hplSubTitle"), HyperLink)
        hplSubTitle.Text = row.Title
        hplSubTitle.NavigateUrl = MainDomain & "mua-ban/" & row.QueryString & "-" & row.CatId & ".html"
        Dim rptThirdCat As Repeater = CType(e.Item.FindControl("rptThirdCat"), Repeater)

        Using Data As New VSEntities
            'Lay toan bo cap con cua Cate

            Dim p = (From q In Data.Categories Where q.ParentId = row.CatId).ToList
            rptThirdCat.DataSource = p
            rptThirdCat.DataBind()

        End Using
    End Sub
    Protected Sub rptThirdCat_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        Dim hplThirdTitle As HyperLink = CType(e.Item.FindControl("hplThirdTitle"), HyperLink)
        hplThirdTitle.Text = row.Title
        hplThirdTitle.NavigateUrl = MainDomain & "mua-ban/" & row.QueryString & "-" & row.CatId & ".html"

    End Sub

End Class
